package top.littlejiang.onlinexam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.littlejiang.onlinexam.common.annotation.Log;
import top.littlejiang.onlinexam.common.base.BasePage;
import top.littlejiang.onlinexam.common.utils.Result;
import top.littlejiang.onlinexam.entity.OlexamClass;
import top.littlejiang.onlinexam.service.OlexamClassService;

import java.io.Serializable;
import java.util.List;

/**
 * @author chen
 * @Description
 * @since 2021/3/12 12:06
 */
@Controller
@RequestMapping("/class")
public class ClassController {
    @Autowired
    OlexamClassService classService;

    /**
     * 分页查询
     * @param page
     * @return
     */
    @PostMapping("/classlist")
    @ResponseBody
    @RequiresPermissions("olexam:basis:class")
    public Result<IPage<OlexamClass>> classList(@RequestBody BasePage<OlexamClass> page){
        IPage<OlexamClass> classPage = classService.getClassListPage(page);
        return Result.ok(classPage);
    }

    /**
     * 保存或更新一条记录
     * @param olexamClass
     * @return
     */
    @Log("保存/更新班级")
    @PostMapping("/saveorupdate")
    @ResponseBody
    @RequiresPermissions("olexam:basis:class")
    public Result saveOrUpdate(@RequestBody OlexamClass olexamClass){
        classService.insertOrUpdate(olexamClass);
        return Result.ok();
    }

    /**
     * 禁用 list
     * @param ids
     * @return
     */
    @Log("禁用班级")
    @PostMapping("/del")
    @ResponseBody
    @RequiresPermissions("olexam:basis:class")
    public Result delete(@RequestBody List<Serializable> ids){
        classService.logicDeleteById(ids);
        return Result.ok();
    }

    /**
     * 获取班级 id+name
     * @return
     */
    @PostMapping("/getall")
    @ResponseBody
    public Result getAll(){
        return Result.ok(classService.getAll());
    }
}
